{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Requirements\n",
    "!pip install nibabel vtk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import k3d\n",
    "import numpy as np\n",
    "from k3d.helpers import download"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import vtk\n",
    "from vtk.util import numpy_support\n",
    "\n",
    "filename = download('https://vedo.embl.es/examples/data/embryo.slc')\n",
    "reader = vtk.vtkSLCReader()\n",
    "reader.SetFileName(filename)\n",
    "reader.Update()\n",
    "vti = reader.GetOutput()\n",
    "\n",
    "bounds = vti.GetBounds()\n",
    "x, y, z = vti.GetDimensions()\n",
    "volume_data = numpy_support.vtk_to_numpy(vti.GetPointData().GetArray(0)).reshape(-1, y, x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "embryo = k3d.volume_slice(volume_data.astype(np.float16), \n",
    "                          color_map=np.array(k3d.basic_color_maps.BlackBodyRadiation, dtype=np.float32), \n",
    "                          slice_z=volume_data.shape[0]//2,\n",
    "                          slice_y=volume_data.shape[1]//2,\n",
    "                          slice_x=volume_data.shape[2]//2,\n",
    "                          bounds=bounds)\n",
    "\n",
    "plot = k3d.plot(camera_mode='volume_sides', grid_visible=False, background_color=0)\n",
    "plot += embryo\n",
    "plot.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('binary_snapshot.k3d', 'wb') as f:\n",
    "    f.write(plot.get_binary_snapshot())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mask = np.empty_like(volume_data, dtype=np.uint8)\n",
    "mask[:, :mask.shape[1]//2, :] = 1\n",
    "mask[:, mask.shape[1]//2:, :] = 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "embryo = k3d.volume_slice(volume_data.astype(np.float16), \n",
    "                          color_map=np.array(k3d.paraview_color_maps.Grayscale, dtype=np.float32), \n",
    "                          slice_z=volume_data.shape[0]//2,\n",
    "                          slice_y=volume_data.shape[1]//2,\n",
    "                          slice_x=volume_data.shape[2]//2,\n",
    "                          active_masks=[1,2],\n",
    "                          mask=mask,\n",
    "                          mask_opacity=0.5,\n",
    "                          bounds=bounds)\n",
    "\n",
    "plot = k3d.plot(camera_mode='volume_sides', grid_visible=False, background_color=0)\n",
    "plot += embryo\n",
    "plot.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  },
  "nbTranslate": {
   "displayLangs": [
    "en",
    "pl"
   ],
   "hotkey": "alt-t",
   "langInMainMenu": true,
   "sourceLang": "pl",
   "targetLang": "en",
   "useGoogleTranslate": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
